import { getPlatformConfig } from '@/api/signUp';

export default {
  computed: {
    rowSpan() {
      return this.showMobilePage ? 24 : 12;
    }
  },
  methods: {
    // 获取动态字段
    getDynamic(detailData, dynamicParentKeys = []) {
      getPlatformConfig(detailData.platformType).then((res) => {
        if (res.data) {
          this.setDynamic(res.data, detailData, dynamicParentKeys);
        }
      });
    },
    // 设置动态字段
    setDynamic(data = [], detailData, dynamicParentKeys = []) {
      const res = data.length && data[0].item || [];
      let configArr = [
        { name: '账号信息', dynamicParentKey: 'moreAccountInfo' },
        { name: '价格信息', dynamicParentKey: 'morePriceInfo' },
        { name: '签约信息', dynamicParentKey: 'handoverKey' },
        { name: '上刊价格', dynamicParentKey: 'journalKey' },
        { name: '成本信息', dynamicParentKey: 'costInfoKey' },
        { name: '更多上刊信息', dynamicParentKey: 'moreJournalKey' }
      ];
      configArr = configArr.filter(config => dynamicParentKeys.includes(config.dynamicParentKey));
      configArr.forEach(item => {
        const findItem = res.find(i => i.name === item.name);
        const newDynamicKey = findItem && findItem.item ? findItem.item : [];
        const oldDynamicData = detailData[item.dynamicParentKey].dynamicKey || [];
        // 回填值
        this.handelDynamic(newDynamicKey, oldDynamicData);
        if (detailData) {
          if (newDynamicKey.length !== oldDynamicData.length) { // 自定义字段配置修改标记为表单变动
            this.modelData.dynamicModify = true;
          }
          this.$set(detailData[item.dynamicParentKey], 'dynamicKey', newDynamicKey);
          const requiredModel = this.getRequired(detailData[item.dynamicParentKey].dynamicKey);
          this.$set(detailData[item.dynamicParentKey], 'requiredModel', requiredModel);
        }
      });
      this.clearValidate();
    },
    handelDynamic(list, oldData) {
      list.forEach(i => {
        const oldItem = oldData.find(j => j.id === i.id);
        if (oldItem) {
          if (i.inputType === 1 && oldItem.inputType === 1) {
            let curOldItem = {};
            if (oldItem.options) {
              curOldItem = oldItem.options.find(j => j.value === oldItem.value);
            }
            const curNewItem = i.options.find(j => j.value === oldItem.value);
            if (curNewItem && curOldItem.label === curNewItem.label) {
              i.value = oldItem.value;
            } else {
              i.value = '';
            }
          } else if ((i.inputType === 1 && oldItem.inputType === 0) || (i.inputType === 0 && oldItem.inputType === 1)) {
            i.value = '';
          } else {
            i.value = oldItem.value;
          }
        }
      });
    },
    getRequired(dynamicKey) { // 获取必填字段
      const requiredModel = {};
      dynamicKey.forEach(i => {
        if (i.required) {
          const type = i.inputType === 0 ? '输入' : '选择';
          const validateType = (msg, item) => {
            return (rule, value, callback) => {
              if (item && (item.value || item.value === 0)) {
                callback();
              } else {
                callback(new Error(msg));
              }
            };
          };
          requiredModel[`${i.name}_${i.id}`] = [{ required: true, trigger: 'change', validator: validateType(`请${type}${i.name}`, i) }];
        }
      });
      return requiredModel;
    },
    getValidateForm(parentRefName, formRefName = 'formRef') {
      if (!this.$refs[parentRefName]) return null;
      if (Array.isArray(this.$refs[parentRefName][formRefName])) {
        return this.$refs[parentRefName][formRefName][0] || null;
      } else {
        return this.$refs[parentRefName][formRefName];
      }
    },
    clearValidate() {
      this.$nextTick(() => {
        const promise = [];
        this.refs.forEach(i => {
          promise.push(this.getValidateForm(i));
        });
        promise.forEach(i => {
          i && i.clearValidate();
        });
      });
    },
    validateForm() {
      const promise = [];
      this.refs.forEach(refName => {
        promise.push(this.$refs[refName].validateForm());
      });
      return new Promise((resolve, reject) => {
        Promise.all(promise).then(res => {
          const validateResult = res.every(item => !!item);
          if (validateResult) {
            resolve(true);
          } else {
            reject();
          }
        }).catch((e) => {
          reject(e);
        });
      });
    }
  }
};
